﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace HospitalSystem
{
    public partial class CMDataManager : System.Web.UI.Page
    {
        CasemanagerServices.CasemanagerReceiverClient casemanager = new CasemanagerServices.CasemanagerReceiverClient();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetPanel();
                getChildList();
            }
        }
        #region InitialData
        public void SetPanel()
        {
            ChildDataPanel.Visible = false ;
            QAViewPanel.Visible = false;
            PlanViewPanel.Visible = false;
        }
        // call service (list of person)
        public void getChildList()
        {
            DataClasses1DataContext DB = new DataClasses1DataContext();
            try
            {
                string[] cidList = casemanager.getProfileList();

                #region get list
                //clean database
                var allData = from a in DB.CMProfileTemps select a;
                DB.CMProfileTemps.DeleteAllOnSubmit(allData);
                try
                {
                    DB.SubmitChanges();
                }
                catch
                {
                    DB = new DataClasses1DataContext();
                }

                foreach (string cid in cidList)
                {
                    CMProfileTemp tmp = new CMProfileTemp();
                    tmp.CID = cid;
                    DB.CMProfileTemps.InsertOnSubmit(tmp);
                    try
                    {
                        DB.SubmitChanges();
                    }
                    catch
                    {
                        DB = new DataClasses1DataContext();
                    }
                }

                var data = from a in DB.CMProfileTemps select a;
                CMDataGridView.DataSource = data;
                CMDataGridView.DataBind();
                #endregion

                foreach (string cid in cidList)
                {
                    XmlDocument doc = new XmlDocument();
                    try
                    {
                        doc.LoadXml(casemanager.getCasemanagerProfile(cid));
                        CDAreceiver.receiveProfileXML(doc);
                    }
                    catch { }
                }
            }
            catch { }
        }

        #endregion
        #region ShowData
        protected void CMInfoGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            DataClasses1DataContext DB = new DataClasses1DataContext();
            string CID = null;
            if (e.CommandName == "ShowQADetails")
            {
                LinkButton LB = (LinkButton)e.CommandSource;
                GridViewRow CurrentRow = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
                //to get the value in the first column
                try
                {
                    CID = CurrentRow.Cells[0].Text;

                    string qa = casemanager.getCasemanagerQuestionare(CID.Trim(), "-1");
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(qa);
                    CDAreceiver.receiveCasemanagerQuestionareXML(doc);

                    var qaData = from a in DB.CMQuestionnaires
                                 where a.CID.Equals(CID.Trim()) && a.CMPlanNo.Equals(CDAreceiver.getPlanNO)
                                 select new
                                 {
                                     CID = a.CID,
                                     CMPlanNo = a.CMPlanNo,
                                     QName = (from q in DB.CMQuestionnaireDefs where q.QNCode.Trim().Equals(a.QCode.Trim()) select q.QNName).FirstOrDefault(),
                                     QValue = a.Qvalue,
                                     QTime = a.Qtime
                                 };
                    QADataGridView.DataSource = qaData;
                    QADataGridView.DataBind();
                }
                catch { }
                QAViewPanel.Visible = true;
                PlanViewPanel.Visible = false;
            }
            else if (e.CommandName == "ShowPlanDetails")
            {
                LinkButton LB = (LinkButton)e.CommandSource;
                GridViewRow CurrentRow = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
                //to get the value in the first column
                try
                {
                    CID = CurrentRow.Cells[0].Text;

                    string plan = casemanager.getCasemanagerPlanning(CID.Trim(), "-1");
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(plan);
                    CDAreceiver.receiveCasemanagerPlanningsXML(doc);

                    var cmPlanData = from a in DB.CMPlans
                                     where a.CID.Equals(CID.Trim()) && a.CMPlanNo.Equals(CDAreceiver.getPlanNO)
                                     select new
                                     {
                                         CID = a.CID,
                                         CMPlanNo = a.CMPlanNo,
                                         PlanActCode = a.PlanActCode,
                                         PlanTime = a.PlanTime
                                     };
                    PlanDataGridView.DataSource = cmPlanData;
                    PlanDataGridView.DataBind();
                }
                catch { }
                QAViewPanel.Visible = false;
                PlanViewPanel.Visible = true;
            }
            try
            {
                Person person = (from a in DB.Persons where a.CID.Equals(CID.Trim()) select a).FirstOrDefault();
                Employee emp = (from a in DB.Employees where a.Staff_id.Equals(person.RecordedStaffID) select a).FirstOrDefault();
                ChildFNameLB.Text = person.FirstName;
                ChildLNameLB.Text = person.LastName;
                ChildIDLB.Text = CID.Trim();
                CMPlanNoLB.Text = CDAreceiver.getPlanNO;
                StaffFNameLB.Text = emp.Firstname;
                StaffLNameLB.Text = emp.Lastname;
                StaffIDLB.Text = emp.Staff_id;
                HostLB.Text = "Casemanager";
                HostIDLB.Text = "001";
                NowDTLB.Text = ((DateTime)person.DateTimeUpdate).ToString("dd - MM - yyyy", CultureInfo.InvariantCulture);
            }
            catch { }
            ChildDataPanel.Visible = true;
        }
        #endregion
        #region refresh
        protected void RefreshButton_Click(object sender, EventArgs e)
        {
            getChildList();
        }

        #endregion
        #region Record or ImportData
        protected void Import_Click(object sender, EventArgs e)
        {
            ImportChildData();
            PlanViewPanel.Visible = false;
            QAViewPanel.Visible = false;
        }
        public void ImportChildData()
        {

        }
        #endregion
    }
}